//引入解析req.body插件
var bodyParser = require('body-parser');
//引入token
var jwt = require('./dao/jwt');

const express = require('express')
const app = express()
const port = 3000



//设置允许跨域访问该服务。
//app.all('*', function(req,res,next){
    //设置允许跨域的域名，*代表允许任意域名跨域
    //res.header('Access-Control-Allow-Origin','*');
    //允许的header类型
    //res.header('Access-Control-Allow-Headers','Content-type');
    //跨域允许的请求方式 
    //res.header('Access-Control-Allow-Methods','*');
    //
    //res.header('Content-Type', 'application/json;charset=utf-8');
    //next();
//});
//设置允许跨域访问该服务。
app.all('*', function(req,res,next){
    //允许访问ip，*为所有
    res.header("Access-Control-Allow-Origin","*");
    res.header("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept");
    res.header("Access-Control-Allow-Credentials", true);
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", '3.2.1');
    //方便返回json
    res.header("Content-Type", "application/json;charset=utf-8");
    if(req.method == 'OPTIONS'){
        //让options请求快速返回
        res.sendStatus(200)
    }else{
        next();
    }
});

//解析前端数据
app.use(bodyParser.json());

//token判断
app.use(function(req,res,next){
    if(typeof(req.body.token) != 'underfined'){
        //处理token匹配
        let token = req.body.token;
        let tokenMatch = jwt.verifyToken(token);
        if(tokenMatch == 1){
            //通过验证
            next();
        }
        else{
            //验证不通过
            res.send({status:300});
        }
    }else{
        next();
    }
})

require('./router/index')(app);

//404错误页面
app.use(function(req,res,next){
	let err = new Error('Not Found');
	err.status = 404;
	next(err);
});

//出现错误处理
app.use(function(err,req,res,next){
	res.status(err.status || 500)
	res.send(err.message);
})

app.listen(port, () => console.log(`您已启动端口： ${port}!`))